package com.example.activitydemo;

import android.content.Intent;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

//1.menu的使用
//2.两个Activity之间的数据传递

/**
 * @author wdl
 */
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button mBtnSecondActivity = findViewById(R.id.btn_second_activity);
        mBtnSecondActivity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String data = "hello second activity";
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                intent.putExtra("SecondActivity", data);
                startActivityForResult(intent, 1);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if (requestCode == 1) {
            if (resultCode == RESULT_OK) {
                String data1 = null;
                if (data != null) {
                    data1 = data.getStringExtra("MainActivity");
                }
                Toast.makeText(MainActivity.this, "第二个活动返回的数据" + data1, Toast.LENGTH_SHORT).show();
            }
        }
    }

    /**
     * 使用menu
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch (id) {
            case R.id.action_browser:
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse("https://www.baidu.com/"));
                startActivity(intent);
                Toast.makeText(this, "点击了浏览百度", Toast.LENGTH_SHORT).show();
                break;
            case R.id.action_phone:
                Intent intent1 = new Intent(Intent.ACTION_DIAL);
                intent1.setData(Uri.parse("tel:10086"));
                startActivity(intent1);
                Toast.makeText(this, "点击了电话10086", Toast.LENGTH_SHORT).show();
                break;
            default:
        }
        return super.onOptionsItemSelected(item);
    }
}
